package com.cnbot.canapp.listener.power;

import android.util.Log;

import com.cnbot.canapp.listener.base.MultiSerialItem;
import com.cnbot.canapp.utils.ICanCode;
import com.dc.android_can.can.CanDataType;

import java.text.DecimalFormat;

/**
 * 描述：
 * 作者：dc on 2018/6/20 14:09
 * 邮箱：597210600@qq.com
 */
public class PowerItem extends MultiSerialItem<PowerImp> {

    public PowerItem(PowerImp powerImp){
        super(powerImp, ICanCode.BUF_PAD_OPEN_CODE, ICanCode.BUF_PAD_CLOSE_CODE, ICanCode.BUF_HEALTHY_OPEN_CODE,
              ICanCode.BUF_HEALTHY_CLOSE_CODE, ICanCode.BUF_ENVIRONMENT_OPEN_CODE, ICanCode.BUF_HEALTHY_OPEN_CODE,
              ICanCode.BUF_HEALTHY_CLOSE_CODE, ICanCode.BUF_ENVIRONMENT_OPEN_CODE, ICanCode.BUF_ENVIRONMENT_CLOSE_CODE,
              ICanCode.BUF_SHUT_DOWN_CODE, ICanCode.BUF_ENVIRONMENT_CLOSE_CODE, ICanCode.BUF_SHUT_DOWN_CODE,
              ICanCode.BUF_RESTART_CODE, ICanCode.BUF_PAD_ELE_CODE, ICanCode.BUF_HEALTHY_ELE_CODE,
              ICanCode.BUF_ENVIRONMENT_ELE_CODE, ICanCode.BUF_VBAT_ELE_CODE);
    }
    @Override
    public void onOrderDispatch(int id) {
        // TODO: 2018/11/19   因底层上报采用的是00 ，需要修改，但在修改之前先采用80调试。!canDataType.dir
        if((byte)id == (ICanCode.BUF_POWER_CODE)  && !canDataType.dir){
//            Log.e(TAG, " 电源传感器板: " + (byte)canDataType.id + "   " + (ICanCode.BUF_POWER_CODE)+ "   " + !canDataType.dir);
            // 电源传感器板
            powerCanRead(canDataType);
        }
    }

    private void powerCanRead(CanDataType canDataType){
        if((byte)canDataType.cmdWord == ICanCode.BUF_PAD_OPEN_CODE){
            Log.e(TAG, "平板电源开启回复");
            mListener.padOpenListener();
        } else if((byte)canDataType.cmdWord == ICanCode.BUF_PAD_CLOSE_CODE){
            Log.e(TAG, "平板电源关闭回复");
            mListener.padCloseListener();
        } else if((byte)canDataType.cmdWord == ICanCode.BUF_HEALTHY_OPEN_CODE){
            Log.e(TAG, "健康板电源开启回复");
            mListener.healthyOpenListener();
        } else if((byte)canDataType.cmdWord == ICanCode.BUF_HEALTHY_CLOSE_CODE){
            Log.e(TAG, "健康板电源关闭回复");
            mListener.healthyCloseListener();
        } else if((byte)canDataType.cmdWord == ICanCode.BUF_ENVIRONMENT_OPEN_CODE){
            Log.e(TAG, "环境板电源开启回复");
            mListener.environmentOpenListener();
        } else if((byte)canDataType.cmdWord == ICanCode.BUF_ENVIRONMENT_CLOSE_CODE){
            Log.e(TAG, "环境板电源关闭回复");
            mListener.environmentCloseListener();
        } else if((byte)canDataType.cmdWord == ICanCode.BUF_SHUT_DOWN_CODE){
            Log.e(TAG, "整机关机指令回复");
            mListener.shutdownListener();
        } else if((byte)canDataType.cmdWord == ICanCode.BUF_RESTART_CODE){
            Log.e(TAG, "重启指令回复");
            mListener.restartListener();
        } else if((byte)canDataType.cmdWord == ICanCode.BUF_PAD_ELE_CODE){
//            Log.e(TAG, "平板电流上报");
            mListener.padEleListener();
        } else if((byte)canDataType.cmdWord == ICanCode.BUF_HEALTHY_ELE_CODE){
//            Log.e(TAG, "健康板电流");
            mListener.healthyEleListener();
        } else if((byte)canDataType.cmdWord == ICanCode.BUF_ENVIRONMENT_ELE_CODE){
//            Log.e(TAG, "环境监测板电流");
            mListener.environmentEleListener();
        }  else if((byte)canDataType.cmdWord == ICanCode.BUF_VBAT_ELE_CODE){
//            mListener.environmentEleListener();
            int count = ((canDataType.buf[1]) & 0xff) << 8 | (canDataType.buf[0] & 0xff);
            DecimalFormat df = new DecimalFormat("0.0");//格式化小数
            String num = df.format((float) (count ) / 100);//返回的是String类型
            double ele = Double.valueOf(num);
//            Log.e(TAG, "VBAT总电流" + ele);
        }
    }
}
